package com.sh4.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.util.UserDataElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.sh4.entity.User;
import com.sh4.entity.UserRole;
import com.sh4.service.UserManagerService;

import net.sf.json.JSON;
import net.sf.json.JSONObject;


@RestController
@RequestMapping(value="userManager")
public class UserManagerController {

	@Autowired
	UserManagerService userManagerService;
	
	@RequestMapping(value="userRole", method = RequestMethod.POST)
	public Map<String, Object> setUserRoel(@RequestBody JSONObject params) {
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "请求成功!";
		int state = userManagerService.addUserRole(params);
		if (state != 1) {
			status = 300;
			msg = "请求失败!";
		}
		result.put("status", status);
		result.put("msg", msg);
		return result;
	}
	
	@RequestMapping(value="creat",method=RequestMethod.POST)
	public Map<String, Object> creatUser(@RequestBody User user) {
		
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "";
		int state = this.userManagerService.insertUser(user);
		msg = state == 1 ? "添加用户成功" : "添加用户失败";
		
		result.put("status", status);
		result.put("msg", msg);
		
		return result;
	}
	
	@RequestMapping(value ="delete",method = RequestMethod.POST)
	public Map<String, Object> deleteUser(@RequestBody User user){
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "删除成功";
		if (user.getId() == null) {
			status = 400;
			msg ="删除失败";
		}
		int state = this.userManagerService.deleteUser(user);
		if (state == 0) {
			status = 400;
			msg = "删除失败";
		}
		result.put("status", status);
		result.put("msg",msg);

		
		return result;
	}
	
	@RequestMapping(value ="all",method = RequestMethod.GET)
	public Map<String, Object> selectedUserAll(){
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "请求成功！";
		List<User> users = this.userManagerService.selectedUserAll();
		if (users == null) {
			status = 300;
			msg = "请求失败";
		}
		result.put("status", status);
		result.put("msg", msg);
		result.put("dataList", users);
		return result;
	}
	
	@RequestMapping(value = "search",method = RequestMethod.POST)
	public Map<String, Object>searchUserObj(@RequestBody JSONObject obj){
		System.out.println(obj);
		Map<String, Object> result = new HashMap<String,Object>();
		
		List<User> list = userManagerService.searchUsersBy(obj);
		
		result.put("msg", list);
		
		return result;
	}
	
	@RequestMapping(value = "modifyPassword",method = RequestMethod.POST)
	public Map<String, Object>modifyPassword(@RequestBody JSONObject reqBody){
		
		Map<String, Object> result = new HashMap<String,Object>();
		
		int status = 200;
		String msg = "修改成功";
		try {
			msg = userManagerService.updatePassword(reqBody);
		} catch (Exception e) {
			status = 300;
			msg = "内部错误";
		}
		result.put("status", status);
		result.put("msg",msg);
		return result;
	}
	
	@RequestMapping(value = "getUserInfo",method = RequestMethod.POST)
	public Map<String, Object>getUserInfo(@RequestBody JSONObject reqBody){
		
		Map<String, Object> result = new HashMap<String,Object>();
		
		int status = 200;
		String msg = "请求成功";
		try {
			result.put("data", userManagerService.getUserInfo(reqBody));
		} catch (Exception e) {
			status = 300;
			msg = "内部错误";
		}
		result.put("status", status);
		result.put("msg",msg);
		return result;
	}
	
	@RequestMapping(value = "updateUserInfo",method = RequestMethod.POST)
	public Map<String, Object>updateUserInfo(@RequestBody JSONObject reqBody){
		
		Map<String, Object> result = new HashMap<String,Object>();
		
		int status = 200;
		String msg = "请求成功";
		try {
			userManagerService.updateUserInfo(reqBody);
		} catch (Exception e) {
			status = 300;
			msg = "内部错误";
		}
		result.put("status", status);
		result.put("msg",msg);
		return result;
	}
	
}
